<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_services_FolderService extends f_core_Service
{
	/**
	 * @var m_backoffice_services_FolderService
	 */
	private static $instance;
	
	/**
	 * Constructor
	 */
	private function __construct()
	{
		// Code if needed...
	}
	
	/**
	 * @return m_backoffice_services_FolderService
	 */
	public static function getInstance()
	{
		if (self::$instance === null)
		{
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	public function getMenuItems($nodeId, $documentModel, $lang, $type = array("type" => ""), $isRoot = true)
	{
        $node = $this->getByUniqueId($nodeId);
		$folders = f_relation_ManagerNew::getInstance($node)->getChilds()->withModel($documentModel)->withLang($lang)->retrieveDocuments();

		if(count($folders) == 0) return "";

		$nbFolder = count($folders);

		if($isRoot)
		{
			$result = '<ul id="folder" class="filetree treeview">';
		}
		else
		{
			$result = "<ul>"."\r\n";
		}

		foreach($folders as $key => $folder)
		{
			if($key == $nbFolder-1)
			{
				$result .= "\t".'<li class="last">';
			}
			else
			{
				$result .= "\t"."<li>";
			}

            switch($type["type"])
            {
                default:
                    $result .= '<span id="folder_'.$folder->getUniqueId().'" class="folder">&nbsp;'.$folder->getLabel().'</span>';
                break;
                
                case "form":
                    $selected = "";
                    if($type["selected"] == $folder->getUniqueId())
                    {
                        $selected = "checked";
                    }

                    $result .= '<span id="folder_'.$folder->getUniqueId().'" class="folder">&nbsp;'.$folder->getLabel().'</span>';
                    $result .= '<input type="'.$type["formType"].'" name="docId" id="check_'.$folder->getUniqueId().'" style="margin: 0px; float: right;" value="'.$folder->getUniqueId().'" '.$selected.'/>';
                break;

                case "link":
                    $result .= '<span id="folder_'.$folder->getUniqueId().'" class="folder">&nbsp;<a href="#'.$folder->getLang().'/'.$folder->getUniqueId().'">'.$folder->getLabel().'</a></span>';
                break;
            }

			$result .= $this->getMenuItems($folder->getUniqueId(), $documentModel, $lang, $type, false);
		}

		$result .= "</li></ul>";

		return $result;
	}
}